// Copyright (c) 2013, yuno. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
//    this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of contributors
//    may be used to endorse or promote products derived from this software
//    without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
package sugarmintcandy.widgetwindow;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.appwidget.AppWidgetManager;
import android.content.DialogInterface;
import android.os.Bundle;

public class ControlWidgetConfigure extends Activity implements
	DialogInterface.OnCancelListener,
	DialogInterface.OnMultiChoiceClickListener,
	DialogInterface.OnClickListener {
    private int mCheckedItems;

    @Override
    @SuppressWarnings("deprecation")
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	mCheckedItems = 31;
	showDialog(R.id.control_widget_configure);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
	super.onRestoreInstanceState(savedInstanceState);
	mCheckedItems = savedInstanceState.getInt("mCheckedItems",
		mCheckedItems);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
	super.onSaveInstanceState(outState);
	outState.putInt("mCheckedItems", mCheckedItems);
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {
	if (id == R.id.control_widget_configure) {
	    AlertDialog.Builder b = new AlertDialog.Builder(this);
	    b.setOnCancelListener(this);
	    b.setCancelable(true);
	    b.setTitle(R.string.widget_name);
	    String[] items = { getString(R.string.open_window),
		    getString(R.string.workspace_1),
		    getString(R.string.workspace_2),
		    getString(R.string.workspace_3),
		    getString(R.string.workspace_4),
		    getString(R.string.workspace_5),
		    getString(R.string.workspace_6),
		    getString(R.string.workspace_7),
		    getString(R.string.workspace_8),
		    getString(R.string.workspace_9),
		    getString(R.string.workspace_10) };
	    boolean[] checks = new boolean[11];
	    for (int i = 0; i < 11; i++) {
		checks[i] = (mCheckedItems & (1 << i)) != 0;
	    }
	    b.setMultiChoiceItems(items, checks, this);
	    b.setPositiveButton(android.R.string.ok, this);
	    b.setNegativeButton(android.R.string.cancel, this);
	    return b.create();
	}
	return super.onCreateDialog(id);
    }

    @Override
    public void onCancel(DialogInterface dialog) {
	setResult(RESULT_CANCELED);
	finish();
    }

    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
	if (isChecked) {
	    mCheckedItems |= 1 << which;
	} else {
	    mCheckedItems &= ~(1 << which);
	}
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
	if (which == DialogInterface.BUTTON_POSITIVE) {
	    int id = getIntent().getIntExtra(
		    AppWidgetManager.EXTRA_APPWIDGET_ID,
		    AppWidgetManager.INVALID_APPWIDGET_ID);
	    if (id != AppWidgetManager.INVALID_APPWIDGET_ID) {
		MainApplication app = (MainApplication) getApplication();
		MainDatabase db = app.getDatabase();
		db.setControlWidgetButtons(id, mCheckedItems);
		ControlWidget.doUpdate(this, id);
	    }
	    setResult(RESULT_OK, getIntent());
	    finish();
	}
	if (which == DialogInterface.BUTTON_NEGATIVE) {
	    setResult(RESULT_CANCELED);
	    finish();
	}
    }
}
